﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataCenter.Models;
using System.Net;
using System.IO;
using KeaoBase;
using DataCenter_DataBase;
using System.Threading;
using DataCenter.Models.WeiXin;

namespace DataCenter.Controllers
{
    public class WeiXinController : Controller
    {

        public string Index()
        {
             ApiDataBase m_Api = new ApiDataBase();

            WeiXinUser weiXinItem = WeiXinComm.GetWeiXinUserByOpenId("");
            tbl_weixin_user userItem = new tbl_weixin_user();
            userItem.id = KeaoUtils.GetGUID();
            userItem.client_code = "";
            userItem.expires_time = DateTime.Today;
            userItem.openid = weiXinItem.openid;
            userItem.user_status = 0;
            userItem.weixin_headimgurl = weiXinItem.headimgurl;
            userItem.weixin_nickname = weiXinItem.nickname;
            m_Api._Db.tbl_weixin_user.Add(userItem);
            return  m_Api._Db.SaveChanges().ToString(); 
        }

        [HttpGet]
        public string WeiXinEcho(string signature, string timestamp, string nonce, string echostr)
        {
            if (Models.WeiXin.WeiXinComm.CheckSign(System.Web.HttpContext.Current, SysConfig._Config.token))
            {
                string echoStr = Request.Params["echostr"];
                return echoStr;
            }
            else
            {
                return string.Empty;
            }
        }

        [HttpPost]
        public string WeiXinEcho()
        {
            if (!Models.WeiXin.WeiXinComm .CheckSign(System.Web.HttpContext.Current, SysConfig._Config.token))
            {
                return string.Empty;
            }

            string xml = string.Empty;
            using (StreamReader reader = new StreamReader(HttpContext.Request.InputStream))
            {
                xml = reader.ReadToEnd();
            }

            KeaoXmlHelper xmlHelper = new KeaoXmlHelper(xml);
            Models.WeiXin.BaseWeiXinMsg msgBase;
            try
            {
                string msgType = xmlHelper.GetFirstNodeInnerString("MsgType");
                switch (msgType)
                {
                    case "text":
                        msgBase = new Models.WeiXin.WeiXinMsg(xmlHelper);
                        break;
                    case "event":
                        string eventType = xmlHelper.GetFirstNodeInnerString("Event");
                        switch (eventType)
                        {
                            case "CLICK":
                                msgBase = new Models.WeiXin.WeiXinEvent_CLICK(xmlHelper);
                                break;
                            case "subscribe":
                                msgBase = new Models.WeiXin.WeiXinEvent_subscribe(xmlHelper);
                                break;
                            case "unsubscribe":
                                msgBase = new Models.WeiXin.WeiXinEvent_unsubscribe(xmlHelper);
                                break;
                            default:
                                msgBase = new Models.WeiXin.BaseWeiXinEvent(xmlHelper);
                                break;
                        }
                        break;
                    default:
                        msgBase = new Models.WeiXin.BaseWeiXinMsg(xmlHelper);
                        break;
                }
                return msgBase.ResponseResult();
            }
            catch (Exception err)
            {
                msgBase = new Models.WeiXin.BaseWeiXinMsg(xmlHelper);
                return msgBase.ReturnTextMsg(string.Format("【{0}】出现问题了,请稍后再试。", err.Message));
            }
        }

        private string Menu()
        {
            Senparc.Weixin.Entities.WxJsonResult delResult = Senparc.Weixin.MP.CommonAPIs.CommonApi.DeleteMenu(Models.WeiXin.WeiXinComm.GetWeiXinAccessToken());
            Senparc.Weixin.MP.Entities.Menu.ButtonGroup bg = new Senparc.Weixin.MP.Entities.Menu.ButtonGroup();

            //二级菜单
            var subButton1 = new Senparc.Weixin.MP.Entities.Menu.SubButton()
            {
                name = "收料查询"
            };
            subButton1.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "10",
                name = "昨日统计"
            });
            subButton1.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "11",
                name = "今日统计"
            });
            subButton1.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "12",
                name = "本月统计"
            });
            subButton1.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "13",
                name = "库存查询"
            });
            bg.button.Add(subButton1);

            var subButton2 = new Senparc.Weixin.MP.Entities.Menu.SubButton()
            {
                name = "出库查询"
            };
            subButton2.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "20",
                name = "昨日统计"
            });
            subButton2.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "21",
                name = "今日统计"
            });
            subButton2.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "22",
                name = "本月统计"
            });
            bg.button.Add(subButton2);

            var subButton3 = new Senparc.Weixin.MP.Entities.Menu.SubButton()
            {
                name = "供应商"
            };
            subButton3.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "31",
                name = "今日统计"
            });
            subButton3.sub_button.Add(new Senparc.Weixin.MP.Entities.Menu.SingleClickButton()
            {
                key = "32",
                name = "今日车辆统计"
            });
            bg.button.Add(subButton3);

            Senparc.Weixin.Entities.WxJsonResult result = Senparc.Weixin.MP.CommonAPIs.CommonApi.CreateMenu(Models.WeiXin.WeiXinComm.GetWeiXinAccessToken(), bg);

            return KeaoUtils.Obj2Json(result);
        }

    }
}